/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatroom.com.frame;

import chatroom.com.entity.UserModel;
import chatroom.com.util.StringUtil;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class RegisterFrame extends javax.swing.JFrame {

    private PrintWriter out;
    private BufferedReader in;

    /**
     * Creates new form RegisterFrame
     */
    public RegisterFrame() {
        initComponents();
    }

    public RegisterFrame(PrintWriter out, BufferedReader in) {
        super("用户注册");
        initComponents();
        this.out = out;
        this.in = in;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        register = new javax.swing.JButton();
        reset = new javax.swing.JButton();
        username = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        password2 = new javax.swing.JPasswordField();
        nick = new javax.swing.JTextField();
        man = new javax.swing.JRadioButton();
        woman = new javax.swing.JRadioButton();
        age = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        city = new javax.swing.JTextField();
        qq = new javax.swing.JTextField();
        email = new javax.swing.JTextField();
        name = new javax.swing.JLabel();
        pass1 = new javax.swing.JLabel();
        pass2 = new javax.swing.JLabel();
        nk = new javax.swing.JLabel();
        ae = new javax.swing.JLabel();
        cy = new javax.swing.JLabel();
        qh = new javax.swing.JLabel();
        el = new javax.swing.JLabel();
        sx = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("用户注册");

        jLabel2.setText("*用户名：");

        jLabel3.setText("*密码  ：");

        jLabel4.setText("*确认密码：");

        jLabel5.setText("*用户昵称：");

        jLabel6.setText("*性别：");

        jLabel7.setText("*年龄：");

        jLabel8.setText("*城市：");

        jLabel9.setText("*QQ：");

        jLabel10.setText("*Email：");

        register.setText("注册");
        register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerActionPerformed(evt);
            }
        });

        reset.setText("重置");
        reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetActionPerformed(evt);
            }
        });

        username.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                usernameFocusLost(evt);
            }
        });

        password.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                passwordFocusLost(evt);
            }
        });

        password2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                password2FocusLost(evt);
            }
        });

        nick.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                nickFocusLost(evt);
            }
        });

        buttonGroup1.add(man);
        man.setText("男");

        buttonGroup1.add(woman);
        woman.setSelected(true);
        woman.setText("女");

        age.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ageFocusLost(evt);
            }
        });

        jLabel11.setForeground(new java.awt.Color(255, 0, 51));
        jLabel11.setText("岁");

        city.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                cityFocusLost(evt);
            }
        });

        qq.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                qqFocusLost(evt);
            }
        });

        email.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                emailFocusLost(evt);
            }
        });

        name.setForeground(new java.awt.Color(102, 102, 255));
        name.setText("请输入用户名,用户名长度为6-15");

        pass1.setForeground(new java.awt.Color(102, 102, 255));
        pass1.setText("请输入您的密码，长度为6-15");

        pass2.setForeground(new java.awt.Color(102, 102, 255));
        pass2.setText("请输入确认密码，需跟密码一致");

        nk.setForeground(new java.awt.Color(102, 102, 255));
        nk.setText("请输入用户昵称，长度不超过15");

        ae.setForeground(new java.awt.Color(102, 102, 255));
        ae.setText("请输入您的实际年龄");

        cy.setForeground(new java.awt.Color(102, 102, 255));
        cy.setText("请输入您目前所在的城市");

        qh.setForeground(new java.awt.Color(102, 102, 255));
        qh.setText("请输入您的QQ号码");

        el.setForeground(new java.awt.Color(102, 102, 255));
        el.setText("方便找回密码，请输入您的QQ邮箱");

        sx.setForeground(new java.awt.Color(102, 102, 255));
        sx.setText("请选择您的性别");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(qq, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addComponent(city, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(man)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(woman))
                    .addComponent(nick)
                    .addComponent(email)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(age, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sx)
                            .addComponent(nk)
                            .addComponent(ae))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(qh)
                            .addComponent(el)
                            .addComponent(cy))
                        .addContainerGap())))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(password, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(username, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(password2, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(name)
                            .addComponent(pass1)
                            .addComponent(pass2)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(reset)
                        .addGap(18, 18, 18)
                        .addComponent(register)))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(173, 173, 173)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(name))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pass1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(password2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(pass2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(nk)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(man)
                    .addComponent(woman)
                    .addComponent(sx))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(age, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel11)
                        .addComponent(ae)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cy)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(qq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(qh)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(el)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reset)
                    .addComponent(register))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void registerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerActionPerformed
        if (username.getText() == null || username.getText().length() == 0 || username.getText().trim().equals("")) {
            name.setText("用户名不能为空，请重新输入");
            name.setForeground(Color.red);
        } else if (username.getText().length() > 15 || username.getText().length() < 6) {
            name.setText("用户名长度应该为6--15，请重新输入");
            username.setText("");
            name.setForeground(Color.red);
        }
        if (StringUtil.turnCharsToString(password.getPassword()).length() > 15 || StringUtil.turnCharsToString(password.getPassword()).length() < 6) {
            pass1.setText("密码长度应该为6-15，请重新输入");
            password.setText("");
            pass1.setForeground(Color.red);
        }
        if (StringUtil.turnCharsToString(password2.getPassword()).length() > 15 || StringUtil.turnCharsToString(password2.getPassword()).length() < 6) {
            pass2.setText("确认密码长度应该为6-15，请重新输入");
            password2.setText("");
            pass2.setForeground(Color.red);
        }
        if (!StringUtil.turnCharsToString(password.getPassword()).equals(StringUtil.turnCharsToString(password2.getPassword()))) {
            pass2.setText("俩次密码输入不一致，请重新输入");
            password2.setText("");
            password.setText("");
            pass2.setForeground(Color.red);
        }
        if (nick.getText().length() > 15 || nick.getText().length() < 3) {
            nk.setText("用户昵称长度应该为3-15，请重新输入");
            nick.setText("");
            nk.setForeground(Color.red);
        }
        if (age.getText().length() == 0) {
            ae.setText("年龄不能为空，请重新输入");
            ae.setForeground(Color.red);
            age.setText("");
        }
        String str = null;
        str = age.getText();
        boolean flag = false;
        for (int i = 0; i < str.length(); i++) {
            for (int j = 0; j <= 9; j++) {
                if ((str.charAt(i) + "").equals(j + "")) {
                    flag = true;
                }
            }
            if (!flag) {
                ae.setText("年龄只能为数字，请重新输入");
                ae.setForeground(Color.red);
                age.setText("");
                return;
            }
        }

        if (city.getText().length() == 0) {
            cy.setText("城市不能为空，请重新输入");
            cy.setForeground(Color.red);
            return;
        }
        str = qq.getText();
        flag = false;
        for (int i = 0; i < str.length(); i++) {
            for (int j = 0; j <= 9; j++) {
                if ((str.charAt(i) + "").equals(j + "")) {
                    flag = true;
                }
            }
            if (!flag) {
                qh.setText("QQ只能为数字，请重新输入");
                qh.setForeground(Color.red);
                qq.setText("");
                return;
            }
        }
        if (email.getText().length() == 0) {
            el.setText("邮箱不能为空，请重新输入");
            el.setForeground(Color.red);
            email.setText("");
        }
        UserModel user = new UserModel();
        user.setAge(Integer.parseInt(age.getText()));
        user.setCity(city.getText());
        user.setEmail(email.getText());
        user.setGuanliyuan(0);
        user.setNick(nick.getText());
        user.setQq(qq.getText());
        user.setSex(man.isSelected() ? "男" : "女");
        user.setUserName(username.getText());
        user.setUserPass(StringUtil.turnCharsToString(password2.getPassword()));
        user.setUserState(1);
        out.println("register&" + UserModel.userToString(user));
        out.flush();
        try {
            String line = in.readLine();
            if (line != null) {
                if (line.equals("registerok")) {
                    JOptionPane.showMessageDialog(null, "恭喜您注册成功，现在可以进行登录！");
                    this.dispose();
                } else if (line.equals("registererror")) {
                    JOptionPane.showMessageDialog(null, "注册失败，请重新注册！");
                    this.resetRegister();
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(RegisterFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_registerActionPerformed

    private void usernameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_usernameFocusLost
        if (evt.getSource() == username) {
            try {
                if (username.getText().length() == 0) {
                    name.setText("用户名不能为空，请重新输入");
                    name.setForeground(Color.red);
                    return;
                }
                out.println("registername&" + username.getText());
                out.flush();
                String line = in.readLine();
                if (line != null) {
                    if (line.equals("have")) {
                        name.setText("用户名已经存在，请重新输入");
                        name.setForeground(Color.red);
                        username.setText("");
                    } else if (line.equals("haveno")) {
                        name.setText("恭喜，用户名可以注册");
                        name.setForeground(Color.blue);
                    }
                }

            } catch (IOException ex) {
            }
        }
    }//GEN-LAST:event_usernameFocusLost

    private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed
        this.resetRegister();
    }//GEN-LAST:event_resetActionPerformed

    private void passwordFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordFocusLost
        if (evt.getSource() == password) {
            if (StringUtil.turnCharsToString(password.getPassword()).length() == 0) {
                pass1.setText("密码不能为空，请重新输入");
                pass1.setForeground(Color.red);
                return;
            } else if (StringUtil.turnCharsToString(password.getPassword()).length() > 15 || StringUtil.turnCharsToString(password.getPassword()).length() < 6) {
                pass1.setText("密码长度应该为6--15，请重新输入");
                pass1.setForeground(Color.red);
                password.setText("");
                return;
            } else {
                pass1.setText("密码输入正确");
                pass1.setForeground(Color.blue);
            }
        }
    }//GEN-LAST:event_passwordFocusLost

    private void password2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_password2FocusLost
        if (evt.getSource() == password2) {
            if (StringUtil.turnCharsToString(password2.getPassword()).length() == 0) {
                pass2.setText("确认密码不能为空，请重新输入");
                pass2.setForeground(Color.red);
                return;
            } else if (StringUtil.turnCharsToString(password2.getPassword()).length() > 15 || StringUtil.turnCharsToString(password2.getPassword()).length() < 6) {
                pass2.setText("确认密码长度应该为6--15，请重新输入");
                pass2.setForeground(Color.red);
                password2.setText("");
                return;
            } else if (!StringUtil.turnCharsToString(password.getPassword()).equals(StringUtil.turnCharsToString(password2.getPassword()))) {
                pass2.setText("确认密码长度应该为6--15应该跟密码一致，请重新输入");
                pass2.setForeground(Color.red);
                password2.setText("");
                return;
            } else {
                pass2.setText("确认密码输入正确");
                pass2.setForeground(Color.blue);
            }
        }
    }//GEN-LAST:event_password2FocusLost

    private void nickFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nickFocusLost
        if (nick.getText().length() == 0) {
            nk.setText("用户昵称不能为空，请重新输入");
            nk.setForeground(Color.red);
            return;
        } else if (nick.getText().length() > 15 || nick.getText().length() < 3) {
            nk.setText("用户昵称长度应该为3-15，请重新输入");
            nk.setForeground(Color.red);
            return;
        } else {
            try {

                out.println("registernick&" + nick.getText());
                out.flush();
                String line = in.readLine();
                if (line != null) {
                    if (line.equals("have")) {
                        nk.setText("用户昵称已经存在，请重新输入");
                        nk.setForeground(Color.red);
                        nick.setText("");
                    } else if (line.equals("haveno")) {
                        nk.setText("恭喜，用户昵称可以使用");
                        nk.setForeground(Color.blue);
                    }
                }

            } catch (IOException ex) {
            }
        }
    }//GEN-LAST:event_nickFocusLost

    private void ageFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ageFocusLost
        if (age.getText().length() == 0) {
            ae.setText("年龄不能为空，请重新输入");
            ae.setForeground(Color.red);
            age.setText("");
        }
        String str = null;
        str = age.getText();
        boolean flag = false;
        for (int i = 0; i < str.length(); i++) {
            for (int j = 0; j <= 9; j++) {
                if ((str.charAt(i) + "").equals(j + "")) {
                    flag = true;
                }
            }
            if (!flag) {
                ae.setText("年龄只能为数字，请重新输入");
                ae.setForeground(Color.red);
                age.setText("");
                return;
            } else {
                ae.setText("年龄输入正确");
                ae.setForeground(Color.blue);
            }
        }
    }//GEN-LAST:event_ageFocusLost

    private void cityFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cityFocusLost
        if (city.getText().length() == 0) {
            cy.setText("城市不能为空，请重新输入");
            cy.setForeground(Color.red);
            return;
        } else {
            cy.setText("城市输入正确");
            cy.setForeground(Color.blue);
        }
    }//GEN-LAST:event_cityFocusLost

    private void qqFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_qqFocusLost
        String str = qq.getText();
        if (str.length() == 0) {
            qh.setText("qq号码不能为空，请认真输入");
            qh.setForeground(Color.red);
        }
        boolean flag = false;
        for (int i = 0; i < str.length(); i++) {
            for (int j = 0; j <= 9; j++) {
                if ((str.charAt(i) + "").equals(j + "")) {
                    flag = true;
                }
            }
            if (!flag) {
                qh.setText("QQ只能为数字，请重新输入");
                qh.setForeground(Color.red);
                qq.setText("");
                return;
            } else {
                qh.setText("qq输入正确");
                qh.setForeground(Color.blue);
            }
        }

    }//GEN-LAST:event_qqFocusLost

    private void emailFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_emailFocusLost
        if (email.getText().length() == 0) {
            el.setText("邮箱不能为空，请重新输入");
            el.setForeground(Color.red);
            email.setText("");
        } else {
            el.setText("Email输入正确");
            el.setForeground(Color.blue);
        }
    }//GEN-LAST:event_emailFocusLost

    public void resetRegister() {
        this.username.setText("");
        this.password.setText("");
        this.password2.setText("");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RegisterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RegisterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RegisterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RegisterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RegisterFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ae;
    private javax.swing.JTextField age;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField city;
    private javax.swing.JLabel cy;
    private javax.swing.JLabel el;
    private javax.swing.JTextField email;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JRadioButton man;
    private javax.swing.JLabel name;
    private javax.swing.JTextField nick;
    private javax.swing.JLabel nk;
    private javax.swing.JLabel pass1;
    private javax.swing.JLabel pass2;
    private javax.swing.JPasswordField password;
    private javax.swing.JPasswordField password2;
    private javax.swing.JLabel qh;
    private javax.swing.JTextField qq;
    private javax.swing.JButton register;
    private javax.swing.JButton reset;
    private javax.swing.JLabel sx;
    private javax.swing.JTextField username;
    private javax.swing.JRadioButton woman;
    // End of variables declaration//GEN-END:variables
}
